from Tkinter import *

#Globals

color = "black"
lastx, lasty = 0, 0

#Definitions
def xy(event):
	global lastx, lasty
	lastx, lasty = event.x, event.y

def setColor(newcolor):
	global color
	color = newcolor
	canvas.dtag("all", "paletteSelected")
	canvas.itemconfigure("palette", outline="white")
	canvas.addtag("paletteSelected", "withtag", "palette%s" % color)
	canvas.itemconfigure("paletteSelected", outline="#999999")


def addLine(event):
	global lastx, lasty
	canvas.create_line((lastx, lasty, event.x, event.y), fill=color, width=5, tags="currentline")
	lastx, lasty = event.x, event.y
	
def doneStroke(event):
	canvas.itemconfigure("currentline", width=1)


#Root

root = Tk()
root.columnconfigure(0,weight=1)
root.rowconfigure(0,weight=1)


#Canvas Setup
canvas = Canvas(root)
canvas.grid(column=0,row=0,sticky=(N, W, E, S))
canvas.bind("<Button-1>",xy)
canvas.bind("<B1-Motion>",addLine)
canvas.bind("<ButtonRelease-1>", doneStroke)


id = canvas.create_rectangle((10, 10, 30, 30), fill="red", tags=("palette", "palettered"))
canvas.tag_bind(id,"<Button-1>", lambda x: setColor("red"))
id = canvas.create_rectangle((10, 35, 30, 55), fill="blue", tags=("palette", "paletteblue"))
canvas.tag_bind(id,"<Button-1>", lambda x: setColor("blue"))
id = canvas.create_rectangle((10, 60, 30, 80), fill="black", tags=("palette", "paletteblack"))
canvas.tag_bind(id,"<Button-1>", lambda x: setColor("black"))

setColor("black")
canvas.itemconfigure("palette", width=5)


#main loop
root.mainloop()
